package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.base.model.PageParams;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseCategoryMapper;
import com.xuecheng.content.mapper.CourseMarketMapper;
import com.xuecheng.content.model.dto.AddCourseDto;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.EditCourseDto;
import com.xuecheng.content.model.dto.QueryCourseParamsDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseCategory;
import com.xuecheng.content.model.po.CourseMarket;
import com.xuecheng.content.service.CourseBaseInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CourseBaseInfoServiceImpl implements CourseBaseInfoService {

    @Autowired
    CourseBaseMapper courseBaseMapper;
    @Autowired
    CourseMarketMapper courseMarketMapper;
    @Autowired
    CourseCategoryMapper courseCategoryMapper;
    @Override
    public PageResult<CourseBase> queryCourseBaseList(PageParams pageParams, QueryCourseParamsDto courseParamsDto) {
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(courseParamsDto.getCourseName()),CourseBase::getName,courseParamsDto.getCourseName());
        queryWrapper.eq(StringUtils.isNotEmpty(courseParamsDto.getAuditStatus()),CourseBase::getAuditStatus,courseParamsDto.getAuditStatus());

        Page<CourseBase> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        Page<CourseBase> pageResult = courseBaseMapper.selectPage(page, queryWrapper);
        List<CourseBase> list=pageResult.getRecords();
        long total = pageResult.getTotal();
        PageResult<CourseBase> pageResultSum=new PageResult<>(list,total,pageParams.getPageNo(),pageParams.getPageSize());

        return pageResultSum;
    }

    @Transactional
    @Override
    public CourseBaseInfoDto createCourseBase(Long compangId,AddCourseDto dto) {
        //合法性校验
        if (StringUtils.isBlank(dto.getName())) {
            throw new XueChengPlusException("课程名称为空");
        }

        if (StringUtils.isBlank(dto.getMt())) {
            throw new XueChengPlusException("课程分类为空");
        }

        if (StringUtils.isBlank(dto.getSt())) {
            throw new XueChengPlusException("课程分类为空");
        }

        if (StringUtils.isBlank(dto.getGrade())) {
            throw new XueChengPlusException("课程等级为空");
        }

        if (StringUtils.isBlank(dto.getTeachmode())) {
            throw new XueChengPlusException("教育模式为空");
        }

        if (StringUtils.isBlank(dto.getUsers())) {
            throw new XueChengPlusException("适应人群为空");
        }

        if (StringUtils.isBlank(dto.getCharge())) {
            throw new XueChengPlusException("收费规则为空");
        }

        //向课程信息表插入信息
        CourseBase courseBase=new CourseBase();
        BeanUtils.copyProperties(dto,courseBase);
        courseBase.setCompanyId(compangId);
        courseBase.setCreateDate(LocalDateTime.now());
        courseBase.setAuditStatus("202002");
        courseBase.setStatus("203001");
        int insert = courseBaseMapper.insert(courseBase);
        if(insert<=0){
            throw new XueChengPlusException("课程添加失败");
        }


        //向市场营销表插入信息
        CourseMarket courseMarketNew=new CourseMarket();
        BeanUtils.copyProperties(dto,courseMarketNew);
        Long courseId=courseBase.getId();
        courseMarketNew.setId(courseId);
        int i=saveCourseMarket(courseMarketNew);
        if(i<=0){
            throw new XueChengPlusException("市场营销信息添加失败");
        }
        return getCourseBaseInfo(courseId);

    }


    public CourseBaseInfoDto getCourseBaseInfo(Long courseId) {
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        if(courseBase==null){
            return null;
        }
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        BeanUtils.copyProperties(courseBase,courseBaseInfoDto);

        CourseMarket courseMarket = courseMarketMapper.selectById(courseId);
        if(courseMarket!=null){
            BeanUtils.copyProperties(courseMarket,courseBaseInfoDto);
        }
        CourseCategory courseCategoryST = courseCategoryMapper.selectById(courseBase.getSt());
        courseBaseInfoDto.setStName(courseCategoryST.getName());
        CourseCategory courseCategoryMT = courseCategoryMapper.selectById(courseBase.getMt());
        courseBaseInfoDto.setMtName(courseCategoryMT.getName());
        return courseBaseInfoDto;
    }

    @Override
    public CourseBaseInfoDto modifyCourseBase(Long companyId,EditCourseDto editCourseDto) {
        CourseBase courseBase = courseBaseMapper.selectById(editCourseDto.getId());
        if(courseBase==null){
            throw new XueChengPlusException("课程不存在");
        }
        if(!companyId.equals(courseBase.getCompanyId())){
            throw new XueChengPlusException("机构不存在");
        }
        BeanUtils.copyProperties(editCourseDto,courseBase);
        courseBase.setChangeDate(LocalDateTime.now());
        int i=courseBaseMapper.updateById(courseBase);

        CourseMarket courseMarket = new CourseMarket();
        BeanUtils.copyProperties(editCourseDto,courseMarket);
        saveCourseMarket(courseMarket);

        CourseBaseInfoDto courseBaseInfo = getCourseBaseInfo(editCourseDto.getId());
        return courseBaseInfo;
    }

    public int saveCourseMarket(CourseMarket courseMarketNew){
         String  charge=courseMarketNew.getCharge();
         if(StringUtils.isEmpty(charge)){
             throw new XueChengPlusException("收费规则没有选择");
         }
         if(charge.equals("201001")){
             if(courseMarketNew.getPrice()==null || courseMarketNew.getPrice().floatValue()<=0){
                 throw new XueChengPlusException("课程收费价格不能为空且价格不能小于0");
             }
         }
         CourseMarket courseMarket = courseMarketMapper.selectById(courseMarketNew.getId());
         if(courseMarket==null){
             return courseMarketMapper.insert(courseMarketNew);
         }else{
             BeanUtils.copyProperties(courseMarketNew,courseMarket);
             courseMarket.setId(courseMarketNew.getId());
             return courseMarketMapper.updateById(courseMarket);
         }
    }

}
